All files / src/services auth.ts

0% Statements 0/33
0% Branches 0/12
0% Functions 0/9
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89                                                                                                                                                                                 
// Authentication service
 
import { apiService } from './api';
import { API_ENDPOINTS } from '@/constants';
import type { TokenPayload } from '@/hooks/useTokenManager';
import {
  LoginRequest,
  LoginResponse,
  TokenResponse,
  MessageResponse,
  ApiResult
} from '@/types';
 
export class AuthService {
  async login(credentials: LoginRequest): Promise<ApiResult<LoginResponse>> {
    return apiService.post<LoginResponse>(API_ENDPOINTS.AUTH.LOGIN, {
      username: credentials.username,
      password: credentials.password,
      totp_code: credentials.totp_code,
      device_id: credentials.device_id});
  }
 
  async refreshToken(): Promise<ApiResult<TokenResponse>> {
    return apiService.post<TokenResponse>(API_ENDPOINTS.AUTH.REFRESH);
  }
 
  async logout(): Promise<ApiResult<MessageResponse>> {
    const result = await apiService.post<MessageResponse>(API_ENDPOINTS.AUTH.LOGOUT);
    
    // Clear local storage regardless of API response
    apiService.clearAuthToken();
    
    return result;
  }
 
  isAuthenticated(): boolean {
    if (typeof window === 'undefined') return false;
    
    const token = localStorage.getItem('iptv_auth_token');
    return !!token;
  }
 
  getStoredUser() {
    if (typeof window === 'undefined') return null;
    
    const userData = localStorage.getItem('iptv_user_data');
    return userData ? JSON.parse(userData) : null;
  }
 
  setStoredUser(user: unknown): void {
    if (typeof window !== 'undefined') {
      localStorage.setItem('iptv_user_data', JSON.stringify(user));
    }
  }
 
  clearStoredUser(): void {
    if (typeof window !== 'undefined') {
      localStorage.removeItem('iptv_user_data');
    }
  }
 
  setAuthToken(token: string): void {
    apiService.setAuthToken(token);
  }
 
  decodeToken(token: string): TokenPayload | null {
    try {
      // JWT tokens have 3 parts separated by dots
      const parts = token.split('.');
      if (parts.length !== 3) {
        return null;
      }
 
      // Decode the payload (second part)
      const payload = parts[1];
      // Add padding if needed
      const paddedPayload = payload + '='.repeat((4 - payload.length % 4) % 4);
      const decoded = atob(paddedPayload);
 
      return JSON.parse(decoded);
    } catch (error) {
      console.error('Failed to decode token:', error);
      return null;
    }
  }
}
 
export const authService = new AuthService();